$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_gctzch_m")
    let dgrid1 = $NG.getCmpApi("p_form_gctzch_d2")
    let dgrid2 = $NG.getCmpApi("p_form_gctzch_d3")
    const getHeadValue = (str) => mstform.getItem(str).getValue()
    const param = parseUrlParams(location.href)
    if (param.sourceid && !mstform.getItem("u_lyid").getValue()) {
        mstform.getItem("u_lyid").setValue(param.sourceid)
    }
    selectTabs('1')
    useValuesChange(() => {
        selectTabs('1')
        isTabs()
        hiddenAttmentch()
    }, "p_form_gctzch_m.u_chzt")
    function isTabs() {
        const bo = mstform.getItem("u_chzt").getValue()
        if (bo == '01') {
            selectTabs("tabPanel_tabItem,tabPanel_tabItem_1")
        } else {
            selectTabs('1')
        }
        setTimeout(() => {
            dgrid1 = $NG.getCmpApi("p_form_gctzch_d2")
            dgrid2 = $NG.getCmpApi("p_form_gctzch_d3")
        }, 200)
    }
    // 进场策划调整
    function selectTabs(key) {
        const u_chzt = getHeadValue("u_chzt")
        const tabs = $NG
            .getCompMap()
            .get("__Tabs__")[0]
            .getApi();

        tabs.updateItems((innerItems, activeKey) => {
            return [...innerItems.filter((item) => {
                if (key.split(",").includes(item.name)) {
                    return item
                }
            })];
        });
        setTimeout(()=>{
            if(page.oType == 'view'||page.oType == 'edit') return
            if (key != '1') {
            dgrid1 = $NG.getCmpApi("p_form_gctzch_d2")
            dgrid2 = $NG.getCmpApi("p_form_gctzch_d3")
            $NG.execServer("SELECT", {
                sql: `select s_tree_id,
             s_tree_no, s_tree_name, s_tree_pid, u_dw, u_je,u_bz from p_form_gctzchjcb_d1`}, (res) => {
                if (res.count == 0) return
                const datas = JSON.parse(res.data).reduce((value, item) => {
                    value.push(item.extendObjects)
                    return value
                }, [])
                const arr = arrayToTree(datas, "s_tree_id", "s_tree_pid")
                dgrid1.clearRows().then(() => {
                    dgrid1.addRows(arr)
                })
            })
            $NG.execServer("SELECT", {
                sql: `select s_tree_id,
             s_tree_no, s_tree_name, s_tree_pid, u_dw, u_je,u_bz from p_form_gctzchjcb_d2`}, (res) => {
                if (res.count == 0) return
                const datas = JSON.parse(res.data).reduce((value, item) => {
                    value.push(item.extendObjects)
                    return value
                }, [])
                const arr = arrayToTree(datas, "s_tree_id", "s_tree_pid")
                dgrid2.clearRows().then(() => {
                    dgrid2.addRows(arr)
                })
            })
        }
        },200)
    }

    // 隐藏Attachment
    // 隐藏附件
    function hiddenAttmentch() {
        const u_chzt = mstform.getItem("u_chzt").getValue()
        const temp = ["u_htjdxgwj", "u_xmchxgwj", "u_swchxgwj",]
        if (!u_chzt) {
            temp.forEach(el => {
                $NG.updateUI((updater) => {
                    updater.fieldSetForm.p_form_gctzch_m[el].setProps({
                        hidden: true
                    })
                })
            })
        } else if (u_chzt == '01') {
            temp.forEach(el => {
                $NG.updateUI((updater) => {
                    updater.fieldSetForm.p_form_gctzch_m[el].setProps({
                        hidden: el == 'u_swchxgwj'
                    })
                })
            })
        } else if (u_chzt == '02') {
            temp.forEach(el => {
                $NG.updateUI((updater) => {
                    updater.fieldSetForm.p_form_gctzch_m[el].setProps({
                        hidden: el !== 'u_swchxgwj'
                    })
                })
            })
        }
    }
    // 转属性结构
    function arrayToTree(items, idField = "id", parentField = "parentId", childrenField = "children") {
        const map = {};
        const roots = [];

        // 先建立 id → item 的映射
        items.forEach(item => {
            map[item[idField]] = { ...item, [childrenField]: [] };
        });

        // 组装树
        items.forEach(item => {
            const parentId = item[parentField];
            if (parentId == null || parentId.length < 2 || parentField == '0') {
                // 根节点
                roots.push(map[item[idField]]);
            } else if (map[parentId]) {
                // 挂到父节点的 children 上
                map[parentId][childrenField].push(map[item[idField]]);
            }
        });

        return roots;
    }
    // 添加明细
    function parseUrlParams(url) {
        // 分离URL的哈希部分
        const hashIndex = url.indexOf('#');
        if (hashIndex === -1) return {};

        const hashPart = url.substring(hashIndex + 1);

        // 分离哈希路径和查询参数
        const questionMarkIndex = hashPart.indexOf('?');
        if (questionMarkIndex === -1) return {};

        const queryString = hashPart.substring(questionMarkIndex + 1);

        // 解析查询参数
        const params = {};
        queryString.split('&').forEach(param => {
            const [key, value] = param.split('=');
            if (key && value !== undefined) {
                params[key] = decodeURIComponent(value);
            }
        });

        return params;
    }
})